<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Document</title>
</head>

<body>
  <script>
    const light = function (time, cb) {
      return new Promise((resolve) => {
        setTimeout(() => {
          console.log(cb);
          resolve();
        }, time);
      });
    };

    function step() {
      Promise.resolve()
        .then(() => {
          return light(1000, "绿");
        })
        .then(() => {
          return light(2000, "黄");
        })
        .then(() => {
          return light(3000, "红");
        })
        .then(() => {
          return step();
        });
    }
    step();
  </script>
</body>

</html>